package com.geega.geetest.common;


import com.alibaba.fastjson.JSON;
import com.geega.geetest.enums.ErrorEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
public class JSONResult<T> implements Serializable {

    private boolean success;

    private String code;

    private String msg;

    private T result;

    public static <T> JSONResult<T> success(String message) {
        return create(true,null, ErrorEnum.SUCCESS.getCode(),message);
    }

    public static <T> JSONResult<T> success() {
        return create(true,  null, ErrorEnum.SUCCESS.getCode(),null);
    }

    public static <T> JSONResult<T> success(T data) {
        return create(true,  data, ErrorEnum.SUCCESS.getCode(),null);
    }

    public static <T> JSONResult<T> success(T data, String message) {
        return create(true, data, ErrorEnum.SUCCESS.getCode(), message);
    }

    public static <T> JSONResult<T> fail(String message) {
        return create(false, null, ErrorEnum.SYSTEM_ERROR.getCode(), message);
    }

    public static <T> JSONResult<T> fail(String code, String message) {
        return create(false, null, code, message);
    }

    // public static <T> JSONResult<T> fail(T data, String message) {
    //     return create(false, data, ErrorEnum.SYSTEM_ERROR.getCode(),null);
    // }

    private static <T> JSONResult<T> create(boolean success, T result, String code, String message) {
        JSONResult<T> JSONResult = new JSONResult<>();
        JSONResult.success = success;
        JSONResult.code = code;
        JSONResult.msg = message;
        JSONResult.result = result;
        return JSONResult;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
